class ApiString {
  static const String bookDetailUrl = 'https://book.douban.com/subject/';
  static const String bookActivitiesJsonUrl =
      'https://book.douban.com/j/home/activities';
  static const String bookExpressJsonUrl =
      'https://book.douban.com/j/home/express_books';
  static const String bookDoubanHomeUrl = 'https://book.douban.com/';
  static const String bookActivityCoverReg = r"https:.*(?='\))"; // 获取背景图片
  static const String bookIdRegExp = r"(?<=/subject/)\d+(?=/)"; // 在链接中获取书籍id
  static const String bookPageRegExp = r"(?<=页数:)\s*\d+"; // 在链接中获取书籍页码
  static const String bookPriceRegExp = r"(?<=定价:)\s*\d+"; // 在链接中获取书籍定价
  static const String authorIdRegExp = r"(?<=/author/)\d+(?=/)"; // 在链接中获取书籍作者id
  static const String bookRateRegExp = r"(?<=allstar)\s*\d"; // 在 class 中获取评分

  static String getBookActivityCover(String? style) {
    if (style == null || style.isEmpty) return '';
    // 使用正则表达式获取背景图片
    return RegExp(bookActivityCoverReg).stringMatch(style) ?? "";
  }

  static String getBookId(String? content, String reg) {
    if (content == null || content.isEmpty) return '';
    // 使用正则表达式获取书籍id
    return RegExp(reg).stringMatch(content) ?? "";
  }

  static int getBookPage(String? text) {
    if (text == null || text.isEmpty) return 0;
    var res = RegExp(ApiString.bookPageRegExp).stringMatch(text);
    if (res == null || res.isEmpty) return 0;
    return int.parse(res);
  }

  static double? getBookPrice(String? text) {
    if (text == null || text.isEmpty) return 0.0;
    var res = RegExp(ApiString.bookPriceRegExp).stringMatch(text);
    if (res == null || res.isEmpty) return 0.0;
    return double.parse(res);
  }

  static double? getBookRate(String? text) {
    if (text == null || text.isEmpty) return 0.0;
    var res = RegExp(ApiString.bookRateRegExp).stringMatch(text);
    if (res == null || res.isEmpty) return 0.0;
    return double.parse(res);
  }
}
